package com.syezon.wifikey.redbag;

import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class BoxItemDecoration extends RecyclerView.ItemDecoration {

    private GridLayoutManager.SpanSizeLookup mSpanSizeLookup;
    private int mGap;

    public BoxItemDecoration(GridLayoutManager.SpanSizeLookup spanSizeLookup, int gap) {
        this.mSpanSizeLookup = spanSizeLookup;
        this.mGap = gap;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int childAdapterPosition = parent.getChildAdapterPosition(view);
        int spanSize = mSpanSizeLookup.getSpanSize(childAdapterPosition);
        if (spanSize == 2) {
            int spanIndex = mSpanSizeLookup.getSpanIndex(childAdapterPosition, 4);
            if (spanIndex == 0) {
                outRect.left = mGap;
                outRect.right = mGap / 2;
                outRect.top = mGap;
            }
            if (spanIndex == 2) {
                outRect.left = mGap / 2;
                outRect.right = mGap;
                outRect.top = mGap;
            }
        } else {
            super.getItemOffsets(outRect, view, parent, state);
        }
    }
}
